#include <iostream>
#include <vector>
#include <exception>
using namespace std;

void exception_or_error(int cmd)
{
    // 除0
    if (cmd == 1)
    {
        int a = 2;
        int b = 0;
        int c;
        if (b == 0) {
            throw std::runtime_error("Division by zero error");
        }
        c = a / b;
        cout << "c = " << c << endl;
    }
    // 栈溢出,数组越界
    if (cmd == 2)
    {
        vector<int> arr = {1,2,3,4,5};
        int idx = 10;
        if (idx >= arr.size()) {
            throw std::range_error("Out of range error");
        }
        arr[idx] = 0;
    }
}

void test_exception(int cmd)
{
    try
    {
        exception_or_error(cmd);
    }
    catch (const std::exception &e)
    {
        cout << "error: " << e.what() << endl;
    }
    catch (...)
    {
        cout << "catch other exception" << endl;
    }
}

int main(void)
{
    test_exception(1);
    test_exception(2);

    return 0;
}

/* 
error: Division by zero error
error: Out of range error

 */